﻿/// <reference path="jquery/jquery-1.9.1.js" />
/// <reference path="admin.js" />
$(function () {
    //添加标签
    $('#btn_term').click(function () {
        var termId = $('#term_id').val();
        var termName = $('#term_name').val();
        var termSlug = $('#term_slug').val();
        var termType = $('#term_type').val();
        var termParent = $('#term_parent').val();
        var termDescription = $('#term_description').val();
        var action = termId == '0' ? 'insert_term' : 'update_term';
        $.post('/handlers/termajax.ashx?action=' + action
            , {
                termId: termId
                , termName: termName
                , termSlug: termSlug
                , termType: termType
                , termParent: termParent
                , termDescription: termDescription
            }
            , function (json) {
                var data = $.parseJSON(json);
                if (data.Status == 0) {
                    $('#term_id').val(data.TermId);
                    showMsg('添加成功。', 'alert-success');
                    loadTerms();
                }
                else
                    showMsg(data.Message, 'alert-error');
            });
    });

    //删除
    $('.delete-term').click(function () {
        var termId = $(this).attr('data-term-id');
        $.post('/handlers/termajax.ashx?action=delete_term', { termId: termId }, function (data) {
            if (data != 'success') return false;
            return true;
        });
    });

    //批量操作
    $('.do-action').click(function () {
        var action = $(this).prev('.sel-action').val();
        if (action == 'edit') {

        } else if (action == 'delete') {
            var ids = new Array;
            $('.cb-item:checked').each(function () { ids[ids.length] = $(this).attr('id').replace('cb_category_', ''); });
            $.post('/handlers/termajax.ashx?action=delete_term', { termId: ids.toString() }, function (res) {
                if (res == 'success') {
                    $(ids).each(function (i, item) {
                        $('#term_' + item).remove();
                    });
                    $('.edit-term').click(edit);
                }
            });
        }
    });

    $('#add_term').click(function () {
        $('#act_name').text($('#act_name').text().replace('修改', '添加新'));
        $('#term_id').val('0');
        $('#term_name').val('');
        $('#term_slug').val('');
        $('#term_description').val('');
        var $sel = $('#term_parent option');
        if ($sel.length > 0)
            $sel[0].selected = true;
        return false;
        $('#term_name').focus();
    });

    $('.edit-term').click(edit);
});
//加载标签数据
function loadTerms() {
    $.post('/handlers/termajax.ashx?action=load_term', { termType: $('#term_type').val() }, function (json) {
        var data = $.parseJSON(json);
        var $list = $('#term_list').empty();
        var $parentList = $('#term_parent').empty().append('<option value="0" selected="selected">无</option>');
        $(data).each(function (i, item) {
            $list.append('<tr id="term_' + item.Id + '" data-parent-id="' + item.ParentId + '"><td class="col-cb"><label for="cb_term_' + item.Id + '" class="hide-text">选择 ' + item.Name + '</label><input id="cb_term_' + item.Id + '" class="cb-item" type="checkbox" /></td><td class="col-name"><a id="edit_term_' + item.Id + '" class="edit-term" href="javascript:;">' + item.Name + '</a></td><td class="col-description">' + item.Description + '</td><td class="col-slug">' + item.Slug + '</td><td class="col-post">' + item.Count + '</td><td class="col-action delete-term close" data-dismiss="alert" data-term-id="' + item.Id + '">×</td></tr>');
            $parentList.append('<option value="' + item.Id + '">' + item.Name + '</option>');
        });
    });
}
//编辑事件
function edit() {
    $('#act_name').text($('#act_name').text().replace('添加新', '修改'));
    var $item = $(this).parent().parent();
    $('#term_id').val($(this).attr('id').replace('edit_term_', ''));
    $('#term_name').val($(this).text().replace(/─+?([^─])/, '$1'));
    $('#term_slug').val($item.children('.col-slug').text());
    $('#term_description').val($item.children('.col-description').text());
    $('#term_parent option').removeAttr('selected');
    var $sel = $('#term_parent option[value="' + $item.attr('data-parent-id') + '"]');
    if ($sel.length>0)
        $sel[0].selected = true;
    return false;
}